<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="清单号" prop="invt_no">
        <el-input
          v-model="queryParams.invt_no"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订购人" prop="buyerName">
        <el-input
          v-model="queryParams.buyerName"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="放行日期" prop="orderDate">
        <el-date-picker
          v-model="queryParams.completeDateTime"
          size="small"
          style="width: 240px"
          value-format="yyyyMMdd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="float: right">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          :disabled="multiple"
          @click="exportOrder"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5" style="float: right">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          style="float: right"
          size="mini"
          :disabled="multiple"
          @click="exportOrderGoods"
        >导出商品
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="清单号" align="center" prop="invt_no" />
      <el-table-column label="放行时间" align="center" prop="completeDateTime" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="订购人" align="center" prop="buyerName" />
      <el-table-column label="实付款" align="center" prop="realPayment" />
      <el-table-column label="清单状态" align="center" :formatter="statusFormat" />
      <el-table-column label="运单号" align="center" prop="bill_no" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="primary"
            @click="see(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 查看详情 -->
    <ClearOrderDetails :show.sync="show" :form="form" />
  </div>
</template>
<script>
import { exportFinishClearOrder, exportFinishClearOrderGoods, finishClearList, orderInfo } from '@/api/order'
export default {
  name: 'Index',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 非多个禁用
      multiple: true,
      // 是否显示弹出层
      dialogVisible: false,
      info: '',
      // 订单状态
      PushInventoryOptions: [
        { 'value': 0, 'label': '未推送' },
        { 'value': 1, 'label': '电子口岸已暂存' },
        { 'value': 2, 'label': '电子口岸申报中' },
        { 'value': 3, 'label': '发送海关成功' },
        { 'value': 4, 'label': '发送海关失败' },
        { 'value': 100, 'label': '海关退单' },
        { 'value': 120, 'label': '海关入库' },
        { 'value': 300, 'label': '人工审核' },
        { 'value': 399, 'label': '海关审结' },
        { 'value': 800, 'label': '放行' },
        { 'value': 899, 'label': '结关' },
        { 'value': 500, 'label': '查验' },
        { 'value': 501, 'label': '扣留移送通关' },
        { 'value': 502, 'label': '扣留移送缉私' },
        { 'value': 503, 'label': '扣留移送法规' },
        { 'value': 599, 'label': '其它扣留' },
        { 'value': 700, 'label': '退运' }
      ],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        orderId: undefined,
        invt_no: undefined,
        buyerName: undefined,
        // 日期范围
        completeDateTime: []
      },
      form: {},
      show: false,
      ids: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单详情 */
    see(row) {
      orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
        if (response.code === 10000) {
          this.form = response.data
          this.show = true
        }
      })
    },
    /** 查询清单列表 */
    getList() {
      this.loading = true
      // console.log(this.queryParams)
      finishClearList(this.queryParams).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          this.list = response.data
          this.total = response.count
          this.loading = false
        }
      })
    },
    /** 状态字典翻译 */
    statusFormat(row, column) {
      let status = row.push_inventory
      for (const d of this.PushInventoryOptions) {
        if (row.push_inventory === d.value) {
          status = d.label
          break
        }
      }
      return status
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        orderId: undefined,
        invt_no: undefined,
        buyerName: undefined,
        // 日期范围
        completeDateTime: []
      }
      this.handleQuery()
    },
    /** 导出订单列表 */
    exportOrder() {
      const that = this
      that.loading = true
      let params = {}
      // exportType 1:选中导出 2：列表导出
      if (that.ids.length > 0) {
        params.ids = that.ids.toString()
      } else {
        params = that.queryParams
      }

      exportFinishClearOrder(params).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          window.open(response.data.url)
        } else {
          that.$message.error(response.message)
        }
        that.loading = false
      })
    },
    /** 导出订单商品列表 */
    exportOrderGoods() {
      const that = this
      this.loading = true
      let params = {}
      // exportType 1:选中导出 2：列表导出
      if (that.ids.length > 0) {
        params.ids = that.ids.toString()
        params.exportType = 1
      } else {
        params = that.queryParams
        params.exportType = 2
      }
      exportFinishClearOrderGoods(params).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          window.open(response.data.url)
        } else {
          that.$message.error(response.message)
        }
        that.loading = false
      })
    },
    /** 提交时间改变 */
    datePick(value) {
      if (value) {
        this.queryParams.startTime = value[0]
        this.queryParams.endTime = value[1]
      }
    },
    /** 选中的项 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.multiple = !selection.length
    }
  }
}
</script>
